<?php
/**
 * Application level Controller
 *
 * This file is application-wide controller file. You can put all
 * application-wide controller-related methods here.
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 */

App::uses('Controller', 'Controller');

/**
 * Application Controller
 *
 * Add your application-wide methods in the class below, your controllers
 * will inherit them.
 *
 * @package		app.Controller
 * @link		http://book.cakephp.org/2.0/en/controllers.html#the-app-controller
 */
class AppController extends Controller {
	public $components = array(
        'Session',
        'Auth' => array(        	
            'loginRedirect' => array(
                'controller' => 'users',
                'action' => 'dashboard'
            ),
            'logoutRedirect' => array(
                'controller' => 'users',
                'action' => 'login'
            ),
            'authenticate' => array(
                'Form' => array(
                    'scope' => array('User.active' => 1, 'User.role'    => 'admin')
                )
            ),
            'authorize' => array('Controller'),
        )
    );
    public $helpers = array('Html','Form');
    public $adminMenu = array(
        	'home'	=>	array(
        		'title'	=>	'Dashboard',
        		'url'	=>  array('controller'	=>	'users', 'action'	=>	'admin_dashboard'),
        		'icon'	=>  'fa fa-home',
        	),
            'recipescategory' => array(
                'title' => 'Recipes category',
                'icon' => 'fa fa-file-text-o',
                'child' => array(
                    'allrecipescategory'   =>  array(
                        'title'     =>  'All recipes category',
                        'icon'      =>  'fa fa-list',
                        'url'       =>  array('controller'  =>  'recipecategories', 'action'   =>  'index'),
                    ),
                    'addrecipescategory'   =>  array(
                        'title'     =>  'Add new <br>&nbsp&nbsp&nbsp recipes category',
                        'icon'      =>  'fa fa-pencil',
                        'url'       =>  array('controller'  =>  'recipecategories', 'action'   =>  'add'),
                    ),

                ),

            ),
            'recipesmain' => array(
                'title' => 'Recipes main',
                'icon' => 'fa fa-file-text-o',
                'child' => array(
                    'allrecipescategory'   =>  array(
                        'title'     =>  'All recipes main',
                        'icon'      =>  'fa fa-list',
                        'url'       =>  array('controller'  =>  'recipemains', 'action'   =>  'index'),
                    ),
                    'addrecipescategory'   =>  array(
                        'title'     =>  'Add new <br>&nbsp&nbsp&nbsp recipes main',
                        'icon'      =>  'fa fa-pencil',
                        'url'       =>  array('controller'  =>  'recipemains', 'action'   =>  'add'),
                    ),

                ),

            ),

            'recipes' =>  array(
                'title' =>  'Recipes',
                'icon'  =>  'fa fa-file-text-o',
                'child' =>  array(
                    'allpost'   =>  array(
                        'title'     =>  'All recipe',
                        'icon'      =>  'fa fa-list',
                        'url'       =>  array('controller'  =>  'recipes', 'action'   =>  'index'),
                    ),
                    'addpost'   =>  array(
                        'title'     =>  'Add new recipe',
                        'icon'      =>  'fa fa-pencil',
                        'url'       =>  array('controller'  =>  'recipes', 'action'   =>  'add'),
                    ),
                ),
            ),
            /*
            'posts' =>  array(
                'title' =>  'Posts',
                'icon'  =>  'fa fa-file-text-o',
                'child' =>  array(
                    'allpost'   =>  array(
                        'title'     =>  'All post',
                        'icon'      =>  'fa fa-list',
                        'url'       =>  array('controller'  =>  'posts', 'action'   =>  'index'),
                    ),
                    'addpost'   =>  array(
                        'title'     =>  'Add new post',
                        'icon'      =>  'fa fa-pencil',
                        'url'       =>  array('controller'  =>  'posts', 'action'   =>  'add'),
                    ),
                ),
            ),
            'options' =>  array(
                'title' =>  'Options',
                'icon'  =>  'fa fa-cog',
                'child' =>  array(
                    'alloption'   =>  array(
                        'title'     =>  'All options',
                        'icon'      =>  'fa fa-list',
                        'url'       =>  array('controller'  =>  'options', 'action'   =>  'index'),
                    ),
                    'addoption'   =>  array(
                        'title'     =>  'Add new option',
                        'icon'      =>  'fa fa-pencil',
                        'url'       =>  array('controller'  =>  'options', 'action'   =>  'add'),
                    ),
                ),
            ),
            */
        	'users'	=>	array(
        		'title'	=>	'User',
        		'icon'	=>	'fa fa-user',
        		'child'	=>	array(
        			'alluser'	=>	array(
        				'title'		=>	'All user',
        				'icon'		=>	'',
        				'url'		=>	array('controller'	=>	'users', 'action'	=>	'index'),
        			),
        			'adduser'	=>	array(
        				'title'		=>	'Add new user',
        	   		    'icon'		=>	'',
        				'url'		=>	array('controller'	=>	'users', 'action'	=>	'add'),
        			),
        		),
        	),
    	);
    public $currentUser;
	public function beforeFilter() 
	{   
        $this->currentUser = $this->Auth->user();     
        $this->set(array('currentUser'  =>  $this->currentUser ));
        
		if ((isset($this->params['prefix']) && ($this->params['prefix'] == 'admin'))) 
		{
			//$this->layout = 'admin';
			$this->set(array('adminMenu'	=>	$this->adminMenu, 
                             'action'	    =>  $this->action,
                             'currentUser'  =>  $this->currentUser
			));
		}
	}
	public function isAuthorized($user) {
	    // Admin can access every action
	    if (isset($user['role']) && $user['role'] === 'admin') {
	        return true;
	    }

	    // Default deny
	    return false;
	}
}
